package com.tensquare.friend.service;

import com.tensquare.friend.client.UserClient;
import com.tensquare.friend.dao.FriendDao;
import com.tensquare.friend.dao.NoFriendDao;
import com.tensquare.friend.pojo.Friend;
import com.tensquare.friend.pojo.NoFriend;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class FriendService {

    @Autowired
    private FriendDao friendDao;

    @Autowired
    private NoFriendDao noFriendDao;

    @Autowired
    private UserClient userClient;

    /**
     * 好友操作
     * @param claims
     * @param friendId 对方id
     * @param type     喜欢0||不喜欢1
     * @return
     */
    public Result ifLike(Claims claims, String friendId, String type) {
        if (claims == null) {
            return new Result(false, StatusCode.ERROR, "权限不足");
        }
        if (friendId == null && "".equals(friendId)) {
            return new Result(false, StatusCode.ERROR, "参数异常");
        }
        // 获取用户Id
        String userId = claims.getId();
        Friend userFriend = friendDao.findByUseridAndFriendid(userId, friendId);
        Friend friend = friendDao.findByUseridAndFriendid(friendId, userId);

        if (type.equals("1")) {
            // 喜欢
            if (friend == null) {
                // 双方都没有操作  单向喜欢
                if (userFriend == null) {
                    userFriend = new Friend();
                    userFriend.setUserid(userId);
                    userFriend.setFriendid(friendId);
                    userFriend.setIslike("0");
                    friendDao.save(userFriend);
                    noFriendDao.deleteByUseridAndFriendid(userId, friendId);
                    // 用户关注+1 好友粉丝+1
                    userClient.updateFanscountTAndFollowcount(userId, friendId, 1);
                    return new Result(true, StatusCode.OK, "喜欢成功");
                } else {
                    noFriendDao.deleteByUseridAndFriendid(userId, friendId);
                    friendDao.updateIsLike("0", userId, friendId);
                    return new Result(true, StatusCode.OK, "喜欢成功, 请勿重复操作");
                }
            } else {
                if (userFriend == null) {
                    // 它喜欢了我 双向喜欢
                    noFriendDao.deleteByUseridAndFriendid(userId, friendId);
                    userFriend = new Friend();
                    userFriend.setUserid(userId);
                    userFriend.setFriendid(friendId);
                    userFriend.setIslike("1");
                    friendDao.save(userFriend);
                    friendDao.updateIsLike("1", friendId, userId);
                    userClient.updateFanscountTAndFollowcount(userId, friendId, 1);
                    userClient.updateFanscountTAndFollowcount(friendId, userId, 1);
                    return new Result(true, StatusCode.OK, "已成为好友");
                }else {
                    friendDao.updateIsLike("1", userId, friendId);
                    friendDao.updateIsLike("1", friendId, userId);
                    noFriendDao.deleteByUseridAndFriendid(userId, friendId);
                    userClient.updateFanscountTAndFollowcount(userId, friendId, 1);
                    userClient.updateFanscountTAndFollowcount(friendId, userId, 1);
                    return new Result(true, StatusCode.OK, "已成为好友, 请勿重复操作");
                }
            }
        } else if(type.equals("0")) {
            // 不喜欢
            NoFriend noUserFriend = noFriendDao.findByUseridAndFriendid(userId, friendId);
            if (noUserFriend != null) {
                if (userFriend != null) {
                    System.out.println(userId);
                    System.out.println(userFriend.getUserid());
                    //friendDao.deleteByUserid(userId);
                    friendDao.deletefriend(userId, friendId);
                }
                userClient.updateFanscountTAndFollowcount(userId, friendId, -1);
                return new Result(false, StatusCode.ERROR, "讨厌成功, 请勿重复操作");
            }
            NoFriend noFriend = new NoFriend();
            noFriend.setUserid(userId);
            noFriend.setFriendid(friendId);
            noFriendDao.save(noFriend);
            if (userFriend != null) {
                //friendDao.deleteByUserid(userId);
                friendDao.deletefriend(userId, friendId);
            }
            userClient.updateFanscountTAndFollowcount(userId, friendId, -1);
            return new Result(true, StatusCode.OK, "讨厌成功");
        }
        // 到这里type不是1也不是0
        return new Result(false, StatusCode.ERROR, "参数异常");
    }

    /**
     * 删除好友
     */
    public Result deleteFriend(Claims claims, String friendId) {
        if (claims == null) {
            return new Result(false, StatusCode.ERROR, "权限不足");
        }
        if (friendId == null && "".equals(friendId)) {
            return new Result(false, StatusCode.ERROR, "参数异常");
        }
        // 获取用户Id
        String userId = claims.getId();

        // 删除好友表中userId到friendId这条数据
        friendDao.deletefriend(userId, friendId);
        // 更新friendId到userId的isLike为0
        friendDao.updateIsLike("0", friendId, userId);
        // 非好友表中添加数据
        NoFriend noFriend = new NoFriend();
        noFriend.setUserid(userId);
        noFriend.setFriendid(friendId);
        noFriendDao.save(noFriend);

        userClient.updateFanscountTAndFollowcount(userId, friendId, -1);
        return new Result(true, StatusCode.OK, "删除成功");
    }
}
